﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rezepto.DatenbankObjekte;
using Rezepto.Enum;

namespace Rezepto
{
    public partial class SiteMaster : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Get Items, which has to be set depending on the user role
            var adminItem = GetMenuItemByTextValue("Admin Bereich");
            NavigationMenu.Items.Remove(adminItem);
            var benutzer = (Benutzer) Session["LoggedIn"];
            if (benutzer != null)
            {
                Login.Visible = false;
                Logout.Visible = true;
                Profil.Visible = true;
                Profil.Text = string.Format("Profil: {0}", benutzer.Benutzername);
                if (benutzer.Rolle == Rolle.Administrator)
                {
                    // set admin item, in case of logged in User is an admin
                    NavigationMenu.Items.Add(adminItem);
                }
            }
            else
            {
                Profil.Visible = false;
                Login.Visible = true;
                Logout.Visible = false;
            }
        }

        private MenuItem GetMenuItemByTextValue(string itemName)
        {
            var menuItem = new MenuItem();
            foreach (MenuItem item in NavigationMenu.Items )
            {
                if (item.Text == itemName)
                {
                    menuItem = item;
                }
            }
            return menuItem;
        }
    }
}
